package com.black.cat.jvm;

import com.beust.jcommander.JCommander;
import com.beust.jcommander.Parameter;

import java.util.List;

/**
 * @Author blackcat
 * @create 2021/8/11 13:42
 * @version: 1.0
 * @description:命令行相关参数
 */
public class Cmd {

    @Parameter(names = {"-?", "-help"}, description = "print help message", help = true)
    boolean helpFlag = false;

    @Parameter(names = "-version", description = "print version and exit")
    boolean versionFlag = false;

    @Parameter(names = {"-cp", "-classpath"}, description = "classpath")
    String classpath;

    @Parameter(names = "-Xjre", description = "path to jre")
    String jre;

    @Parameter(description = "main class and args")
    List<String> mainClassAndArgs;

    boolean ok;

    String getMainClass() {
        return mainClassAndArgs != null && !mainClassAndArgs.isEmpty()
                ? mainClassAndArgs.get(0)
                : null;
    }

    List<String> getAppArgs() {
        return mainClassAndArgs != null && mainClassAndArgs.size() > 1
                ? mainClassAndArgs.subList(1, mainClassAndArgs.size())
                : null;
    }

    public  static Cmd parse(String[] argv) {
        Cmd cmd = new Cmd();
        try {
            JCommander.newBuilder().addObject(cmd).args(argv).build();
            cmd.ok = true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return cmd;
    }
}
